package com.stone.controller.admin;


import com.stone.bean.AppResponse;
import com.stone.bean.dto.LoginDTO;
import com.stone.bean.dto.SysUserPasswordDTO;
import com.stone.bean.vo.LoginVO;
import com.stone.constant.Const;
import com.stone.service.LoginService;
import com.stone.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/admin/auth")
@Api(tags = "后台登录接口")
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private SysUserService sysUserService;

    @GetMapping("/imageCode")
    @ApiOperation("获取登录验证码")
    public AppResponse createImageCode(HttpServletRequest request) {
        String imgByteStr = loginService.creatImageCode(request);
        imgByteStr = imgByteStr.replaceAll("\r|\n", "");
        return AppResponse.success().setRecord(imgByteStr);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录授权")
    public AppResponse<LoginVO> login(@RequestBody @Valid LoginDTO dto, HttpServletRequest request) {
        LoginVO loginVo = loginService.login(dto, request);
        return AppResponse.success().setRecord(loginVo);
    }

    @GetMapping("/logout")
    @ApiOperation("用户登出")
    public AppResponse logout(@RequestHeader(value = Const.AUTH) String token) {
        loginService.logout(token);
        return AppResponse.success();
    }

    @PostMapping("/passwordUpdate")
    @ApiOperation("用户密码修改")
    public AppResponse updateUserPassword(@RequestBody SysUserPasswordDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        sysUserService.updateUserPassword(dto, token);
        return AppResponse.success();
    }
}
